﻿
namespace System.Security.Cryptography
{
    /// <summary>
    /// Represents the abstract class from which all implementations of keyed hash
    /// algorithms must derive.
    /// </summary>
    public abstract class KeyedHashAlgorithm : HashAlgorithm
    {
        /// <summary>
        /// The key to use in the hash algorithm.
        /// </summary>
        protected byte[] KeyValue;

        /// <summary>
        /// Initializes a new instance of the ConnectMyInfo.Compact.Net.KeyedHashAlgorithm
        /// class.
        /// </summary>
        protected KeyedHashAlgorithm() { }

        /// <summary>
        /// Gets or sets the key to use in the hash algorithm.
        /// </summary>
        public virtual byte[] Key
        {
            get { return (byte[])this.KeyValue.Clone(); }
            set
            {
                if (base.State != 0)
                {
                    throw new CryptographicException("Cryptography_HashKeySet");
                }

                this.KeyValue = (byte[])value.Clone();
            }
        }

        /// <summary>
        /// Releases the unmanaged resources used by the System.Security.Cryptography.KeyedHashAlgorithm
        /// and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.KeyValue != null)
                {
                    Array.Clear(this.KeyValue, 0, this.KeyValue.Length);
                }

                this.KeyValue = null;
            }

            base.Dispose(disposing);
        }
    }
}